package com.augurit.gzcgwsp.ywcmn.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.ywcmn.convert.YwComYsgsConvertor;
import com.augurit.gzcgwsp.ywcmn.dao.YwComYsgsDao;
import com.augurit.gzcgwsp.ywcmn.entity.YwComQyxx;
import com.augurit.gzcgwsp.ywcmn.entity.YwComYsgs;
import com.augurit.gzcgwsp.ywcmn.service.IYwComYsgsService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComYsgsForm;

@Service
@Transactional
public class YwComYsgsServiceImpl implements IYwComYsgsService{

	@Autowired
	private YwComYsgsDao ywComYsgsDao;
	
	@Transactional(readOnly = true)
	public YwComYsgsForm get(Long id) {
		YwComYsgs entity = ywComYsgsDao.get(id);
		return YwComYsgsConvertor.convertVoToForm(entity);
	}
	
	@Transactional(readOnly = true)
    public YwComYsgsForm getFormByYsgs(String ysgs,String templateCode) {
        YwComYsgs entity = ywComYsgsDao.findUnique("from YwComYsgs t where t.ysgs = '" + ysgs+ "' and t.templateCode='"+templateCode+"'");
        if (entity == null) return null;
        return YwComYsgsConvertor.convertVoToForm(entity);
    }
	
	public void delete(Long... ids) {
		ywComYsgsDao.delete(ids);
	}

	public void save(YwComYsgsForm... forms) {
		if(forms != null)
			for(YwComYsgsForm form : forms)
				this.save(form);
	}
	
	public void update(YwComYsgsForm form) {
	    YwComYsgsForm dbform = null;
	    if (form.getId() != null && form.getId() != 0) dbform = this.get(form.getId());
	    
	    if (dbform == null) {
	        this.save(form);
	    } else {
	        new DozerBeanMapper().map(form, dbform);
            this.save(dbform);
	    }
	}
	
	public void save(YwComYsgsForm form){
		
		if(form != null){
			YwComYsgs entity = null;
			
			if(form != null && form.getId() != null){
				entity = ywComYsgsDao.getYwComYsgs(form.getId());
				if(entity.getId()==null){
					entity = new YwComYsgs();
					entity.setId(form.getId());
				}
			}else{
				entity = new YwComYsgs();
			}
			
			YwComYsgsConvertor.convertFormToVo(form, entity);
			
			ywComYsgsDao.save(entity);
			
			form.setId(entity.getId());
		}
	}

	@Transactional(readOnly = true)
	public Page<YwComYsgsForm> search(Page<YwComYsgsForm> page, YwComYsgsForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from YwComYsgs ps where 1=1");
		List values = new ArrayList();
		
		if(form != null){
		    if (StringUtils.isNotBlank(form.getYsgs())) {
		        hql.append(" and ps.ysgs like '%" + form.getYsgs() + "%'");
		    }
		    if(StringUtils.isNotBlank(form.getTemplateCode())){
		    	if(form.getTemplateCode().contains("jzljczz")){//余泥处--处置证
		    		hql.append(" and ps.templateCode like '%jzljczz%'");
		    	}else{
		    		hql.append(" and ps.templateCode = '" + form.getTemplateCode() + "'");
		    	}
		    }
		}
		hql.append(" order by ps.sqrq desc ");
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		ywComYsgsDao.findPage(pg, hql.toString(), values);
		
		List<YwComYsgsForm> list = YwComYsgsConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	@Transactional(readOnly = true)
	public Page<YwComYsgsForm> search(Page<YwComYsgsForm> page, List<PropertyFilter> filters) {
		Page<YwComYsgs> pg = PageUtils.newPageInstance(page);
		
		ywComYsgsDao.findPage(pg, filters);
		
		List<YwComYsgsForm> list = YwComYsgsConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	public void updateYwComYsgs(YwComYsgsForm form) {
	    YwComYsgsForm dbform = null;
	    if (form.getId() != null && form.getId() != 0)
	    	dbform = this.get(form.getId());
	    if (dbform != null) {
			YwComYsgs entity = null;
			entity = ywComYsgsDao.getYwComYsgs(dbform.getId());
			YwComYsgsConvertor.convertFormToVo(form, entity);
			if(entity!=null){
				ywComYsgsDao.updateYwComYsgs(entity);
			}
	    }
	}

    public YwComYsgs getYwComYsgs(Long qyid, String acceptNo) {
        return ywComYsgsDao.getYwComYsgs(qyid);
    }

    public void saveEntity(YwComYsgs entity) {
       this.ywComYsgsDao.save(entity);
        
    }

	public Map<Long, String> getYsgs() {
	 String sql="select t.id,t.ysgs from "+YwComYsgs.class.getName()+" t";
	 List<Object[]> list=this.ywComYsgsDao.find(sql);
	 Map<Long,String> map=new HashMap<Long,String>();
	 for(Object[] objs:list){
		 Long id=(Long) objs[0];
		 String ysgs=(String) objs[1];
		 map.put(id, ysgs);
	 }
	 return map;
	 
	}

	/**
	 * 保存企业信息返回id
	 */
	public Long saveEnterpriseInfo(YwComYsgsForm form) {
		YwComYsgs entity =new YwComYsgs();
		
		YwComYsgsConvertor.convertFormToVo(form, entity);
		
		return ywComYsgsDao.saveEntity(entity);
	}

	public YwComYsgs getEntity(String ysgs, String tempcode) {
		String sql=" from "+YwComYsgs.class.getName()+" where ysgs='"+ysgs+"'"+" and templateCode='"+tempcode+"'";
		List<YwComYsgs> list=this.ywComYsgsDao.find(sql);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

	public YwComYsgsForm getFormByYsgs(String ysgs) {
		YwComYsgs entity = ywComYsgsDao.findUnique("from YwComYsgs t where t.ysgs = '" + ysgs+ "'");
        if (entity == null) return null;
        return YwComYsgsConvertor.convertVoToForm(entity);
	}

	@Override
	public Page<YwComYsgsForm> findYsgs(Page<YwComYsgsForm> page, YwComYsgsForm form) {
		
		return null;
	}

	@Override
	public Page<YwComYsgsForm> searchData(Page<YwComYsgsForm> page, YwComYsgsForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from YwComQyxx ps where 1=1");
		List values = new ArrayList();
		
		if(form != null){
		    if (StringUtils.isNotBlank(form.getYsgs())) {
		        hql.append(" and ps.sqdw like '%" + form.getYsgs() + "%'");
		    }
		    if(StringUtils.isNotBlank(form.getTemplateCode())){
		    	if(form.getTemplateCode().contains("jzljczz")){//余泥处--处置证
		    		hql.append(" and ps.templateCode like '%jzljczz%'");
		    	}else{
		    		hql.append(" and ps.templateCode = '" + form.getTemplateCode() + "'");
		    	}
		    }
		}
		hql.append(" order by ps.id desc ");
		//hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		ywComYsgsDao.findPage(pg, hql.toString(), values);
		
		List<YwComYsgsForm> list = this.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	private List<YwComYsgsForm> convertVoListToFormList(List result) {
		if(result != null && result.size() > 0) {
			List<YwComYsgsForm> ywComYsgsFormList = new ArrayList<YwComYsgsForm>();
			for(int i=0; i<result.size(); i++) {
				YwComQyxx form=(YwComQyxx) result.get(i);
				YwComYsgsForm ysgs=new YwComYsgsForm();
				ysgs.setYsgs(form.getSqdw());
				ysgs.setYsgsLxr(form.getYwlxr());
				ysgs.setYsgsLxrLxdh(form.getYwlxrtel());
				ywComYsgsFormList.add(ysgs);
			}
			return ywComYsgsFormList;
		}
		return null;
	}
}
